<?php
/*
XML File of Comments from CMS Made Simple for Import into DISQUS
Author:  Stephen R. Owens  (www.Studio-Owens.com)
Version: 1.0.1  [June 2, 2012]

Creates a Generic WXR XML file of comments from the Comments Module of CMS Made Simple for importing into the disqus.com service.
Copyright (C) 2012 Stephen R. Owens

LICENSE:
This software is licensed under the GNU GPL version 3.0 or later.
http://www.gnu.org/licenses/gpl-3.0-standalone.html

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

# This file needs to be uploaded to the directory on your server when the CMS Made Simple "config.php" file is located.
require('config.php');

# Database connection
$connection = mysql_connect($config['db_hostname'], $config['db_username'], $config['db_password']) or die(mysql_error());
$db = mysql_SELECT_db($config['db_name'], $connection);
db_query("set character_set_client='utf8'");
db_query("set character_set_results='utf8'");
db_query("set collation_connection='utf8_general_ci'");

$xml_txt = '';

# Begin the XML tags
$xml_txt .= xml_open();

# Gather all the pages from the comments module
$sql = "SELECT DISTINCT `page_id`, `module_name` FROM ".$config['db_prefix']."module_comments ORDER BY `module_name` ASC, `page_id` ASC";
$rows = db_query_arr($sql);

# When comments are found
if(is_array($rows)) {
  foreach ($rows as $row) {
    # Using a switch to get page info from the database
    # Depending upon your CMS Made Simple setup you may need to change the cases or add more cases to the switch below
    switch($row['module_name']) {
      
      # The News module is probably the most common module used in conjunction with the Comments module
      case 'News':
        $sql = "SELECT DISTINCT `news_title` AS 'title', `news_url` AS 'link', `news_date` AS 'date' 
        FROM ".$config['db_prefix']."module_news 
        WHERE `news_id` = '".$row['page_id']."' 
        LIMIT 1";
        $results = db_query_arr($sql);
        $arr = $results[0];
        # Make sure there is news article data for these comments. The news article may have been deleted.
        if (isset($arr['title']) && trim($arr['title']) != '' && $arr['title'] != Null) {
          $arr_page['title'] = $arr['title'];
          $arr_page['link'] = $config['root_url'].'/'.$arr['link'].$config['page_extension'];
          $arr_page['identifier'] = $arr['link'].$config['page_extension'];
          $arr_page['date'] = $arr['date'];
        } else {
          # No title for this news article was found so set blank to skip exporting the related comments
          $sql = '';
          $arr_page = array();
        }
        break;
      
      # Every CMS Made Simple site has pages so this was a natural inclusion for export
      case 'Pages':
        $sql = "SELECT DISTINCT `content_name` AS 'title', `hierarchy_path` AS 'link', `create_date` AS 'date' 
        FROM ".$config['db_prefix']."content 
        WHERE `content_id` = '".$row['page_id']."' 
        LIMIT 1";
        $results = db_query_arr($sql);
        $arr = $results[0];
        # Make sure there is page data for these comments. The page may have been deleted.
        if (isset($arr['title']) && trim($arr['title']) != '' && $arr['title'] != Null) {
          $arr_page['title'] = $arr['title'];
          $arr_page['link'] = $config['root_url'].'/'.$arr['link'].$config['page_extension'];
          $arr_page['identifier'] = $arr['link'].$config['page_extension'];
          $arr_page['date'] = $arr['date'];
        } else {
          # No title for this page was found so set blank to skip exporting the related comments
          $sql = '';
          $arr_page = array();
        }
        break;
      
      default:
        # Setting the query to blank to let the script know that no actions should be taken on unknown module names
        $sql = '';
        $arr_page = array();
        break;
    }
    # When a query is set, execute and build the required XML tags
    if ($sql != '') {
      # Start a new item
      $xml_txt .= '<item>';
      # Add the pages general information
      $xml_txt .= xml_comment_page($arr_page);
      # Add ALL the comments for the page
      $xml_txt .= xml_comment_details($row, $config);
      # End the item
      $xml_txt .= '</item>';
    }
  }
}

# Close the XML tags
$xml_txt .= xml_close();

# Trim blanks from beginning and end of the XML string
$xml_txt = trim($xml_txt);

# Print the XML file to the screen
echo $xml_txt;

#------
# FUNCTIONS
function db_query($sql) {
  $results = mysql_query($sql) or die($sql.' ----- '.mysql_error());
  return $results;
}

function db_query_arr($sql) {
  $results = mysql_query($sql) or die($sql.' ----- '.mysql_error());
  if(is_bool($results)) {
    return $results;
  } else {
    $final_arr = array();
    while ($row = mysql_fetch_array($results)) {
      $final_arr[] = $row;
    }
    return $final_arr;
  }
}

function xml_open() {
  $str = '
<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:dsq="http://www.disqus.com/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wp="http://wordpress.org/export/1.0/">
<channel>
  ';
  return $str;
}

function xml_close() {
  $str = '
</channel>
</rss>
  ';
  return $str;
}

function xml_comment_page($arr) {
  $str = '
  <title>'.$arr['title'].'</title>
  <link>'.$arr['link'].'</link>
  <dsq:thread_identifier>'.$arr['identifier'].'</dsq:thread_identifier>
  <wp:post_date_gmt>'.date('Y-m-d H:i:s', strtotime($arr['date'])).'</wp:post_date_gmt>
  <wp:comment_status>open</wp:comment_status>
  ';
  return $str;
}

function xml_comment_details($arr, $config) {
  $str = '';
  $sql = "SELECT DISTINCT `comment_id`, `comment_author`, `author_email`, `author_website`, `ip`, `comment_date`, `comment_data`, `active` FROM ".$config['db_prefix']."module_comments WHERE module_name = '".$arr['module_name']."' AND page_id = '".$arr['page_id']."' ORDER BY `comment_id` ASC";
  $cm_arr = db_query_arr($sql);
  if (is_array($cm_arr)) {
    foreach ($cm_arr as $cmt) {
      $str .= '
  <wp:comment>
    <wp:comment_id>'.$cmt['comment_id'].'</wp:comment_id>
    <wp:comment_author>'.$cmt['comment_author'].'</wp:comment_author>
    <wp:comment_author_email>'.$cmt['author_email'].'</wp:comment_author_email>
    <wp:comment_author_url>'.$cmt['author_website '].'</wp:comment_author_url>
    <wp:comment_author_IP>'.$cmt['ip'].'</wp:comment_author_IP>
    <wp:comment_date_gmt>'.date('Y-m-d H:i:s', strtotime($cmt['comment_date'])).'</wp:comment_date_gmt>
    <wp:comment_content><![CDATA['.$cmt['comment_data'].']]></wp:comment_content>
    <wp:comment_approved>'.$cmt['active'].'</wp:comment_approved>
    <wp:comment_parent>0</wp:comment_parent>
  </wp:comment>
      ';
    }
  }
  return $str;
}
?>